<template>
	<view class="weChat">
		<template v-if="weChatStatus">
			<view @click="clickDisconnectConnectId">已绑定，点我解绑</view>
		</template>
		<template v-else>
			<view @click="clickWeChatNesting">未绑定，点我绑定</view>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				weChatStatus:false
			}
		},
		onLoad() {
			this.getUser();
		},
		methods: {
			// 请求用户
			getUser(){
				this.$_Request(
					"GET","/api/v2/account/detail",{},
					(res) => {
						this.weChatStatus = res.data.data.detail.connects[0].connected
					}
				)
			},
			// 绑定微信
			clickWeChatNesting(){
				uni.showLoading();
				uni.login({
					provider:'weixin',
					success: (res) => {
						this.weixinCode = res.code;
						this.$_Request(
							"POST","/api/wechat-login/mini-program/oauth",
							{
								"code":this.weixinCode
							},
							(res_) => {
								uni.hideLoading();
								if(res_.data.code == 0){
									uni.showToast({
										title:res_.data.message,
										icon:"none",
										duration:1000
									});
									setTimeout(()=>{
										uni.switchTab({
											url:"/pages/my/my"
										})
									},1000);
								}else{
									uni.showToast({
										title:res_.data.message,
										icon:"none",
										duration:1000
									});
								}
							}
						)
					}
				})
			},
			// 点击，解绑微信
			clickDisconnectConnectId(){
				uni.showLoading();
				this.$_Request(
					"PUT","/api/v2/account/edit",
					{
						"disconnectConnectId":25,
					},
					(res) => {
						if(res.data.code == 0){
							uni.showToast({
								title:res.data.message,
								icon:"none",
								duration:1000
							});
							setTimeout(()=>{
								uni.switchTab({
									url:"/pages/my/my"
								})
							},1000);
						}else{
							uni.showToast({
								title:res.data.message,
								icon:"none",
								duration:1000
							});
						}
					}
				)
			},
		}
	}
</script>

<style lang="scss">
.weChat{
	view{
		width: 700rpx;
		height: 200rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background: $LanSe;
		color: #FFF;
		border-radius: 10rpx;
		margin: 25rpx;
	}
}
</style>
